define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shopcharge/index' + location.search,
                    add_url: 'shop/shopcharge/add',
                    edit_url: 'shop/shopcharge/edit',
                    del_url: 'shop/shopcharge/del',
                    multi_url: 'shop/shopcharge/multi',
                    import_url: 'shop/shopcharge/import',
                    table: 'shop_charge',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                console.log(data);
                //这里我们手动设置底部的值
                $("#one_t").text(data.extend.one);
                $("#two_t").text(data.extend.two);

            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                singleSelect:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('用户名')},
                        {field: 'user.mobile', title: __('手机号')},
                        {field: 'order_no', title: __('订单号'), operate: 'LIKE'},
                        {field: 'amount', title: __('支付金额'), operate:false},
                        {field: 'status', title: __('状态'), searchList: {"paying":__('支付中'),"payed":__('已付款'),'refund':'已退款'}, formatter: Table.api.formatter.status},
                        {field: 'shop_id', title: __('店铺ID')},
                        {field: 'shop.name', title: __('店铺')},
                        {field: 'desc', title: __('套餐'),operate: false},

                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,



                            buttons: [
                                {
                                    name: 'list',
                                    title: __('退款'),
                                    text:'退款',
                                    // classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    url: function (row) {
                                        return "shop/shopcharge/refund?id=" + row['id'];
                                    },
                                    visible:function (row) {
                                        if(row.status === 'refund'){
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    },
                                    refresh: true,
                                },
                                // {
                                //     // name: 'list',
                                //     // title: __('退款'),
                                //     // text:'退款',
                                //     // classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     // confirm: '是否退款',
                                //     // url: function (row) {
                                //     //     return "shop/shopcharge/refund?id=" + row['id'];
                                //     // }
                                //
                                //     name: 'list',
                                //     title: __('退款'),
                                //     text: '退款',
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     url: function (row) {
                                //         return "shop/shopcharge/add?id=" + row['id'];
                                //     }
                                // },
                                {
                                    name: 'list',
                                    title: __('入账明细'),
                                    text:'店铺入账明细',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "money/shoporderprofit/index?order_id=" + row['id']+"&type=charge";
                                    }
                                },

                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refund:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
